public class test3 {
    public void reorderList(ListNode head) {
        // 处理null
        if (head == null || head.next == null) {
            return;
        }
        ListNode slow = head;// 最后会指向中间节点
        ListNode fast = head;
        // 利用快慢指针找到中间节点
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        // 将后半部分链表进行反转
        ListNode newHead = new ListNode();
        ListNode cur = slow.next;
        slow.next = null;// 分成两个链表
        // 头插法
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = newHead.next;
            newHead.next = cur;
            cur = next;
        }
        // 将前半部分和反转后的部分进行拼接
        // 后半部分从newHead的后一个节点进行拼接
        ListNode result = new ListNode();
        // 当后半部分为null时,就代表拼接完了
        ListNode prev = result;
        ListNode cur1 = head, cur2 = newHead.next;
        while (cur1 != null) {
            prev.next = cur1;
            prev = cur1;
            cur1 = cur1.next;
            if (cur2 != null) {
                prev.next = cur2;
                prev=cur2;
                cur2 = cur2.next;
            }
        }
    }
}
